<template>
  <div>
    <basic-page-vxe
      ref="basePage"
      list-api="querySysMenu"
      :search-form="basicData.searchForm"
      :search-handle="basicData.searchHandle"
      :columns="basicData.columns"
      :show-pagination="false"
      :is-tree-data="true"
    />
    <!--    新增、删除 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="210px" style="width: 100%">
        <el-form-item label="角色编码" prop="roleCode">
          <el-select v-model="temp.roleCode" :disabled="dialogStatus==='update'" placeholder="请选择" clearable style="width: 100%">
            <el-option v-for="item in roleCodeList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="过滤数据字段，以英文逗号隔开(比如name,createTime)" prop="dataField">
          <el-input v-model="temp.dataField" placeholder="请输入" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createData()">
          确定
        </el-button>
      </div>
    </el-dialog>
    <el-drawer
      title="数据权限"
      :visible.sync="drawerTableVisible"
      direction="rtl"
      size="60%"
    >
      <el-button style="margin-left: 10px" type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
      <el-table :data="roleFieldDataSourceList">
        <el-table-column label="角色编码" prop="roleCode" min-width="30%" />
        <el-table-column label="过滤数据字段，以英文逗号隔开(比如name,createTime)" prop="dataField" />
        <el-table-column label="操作" align="center" min-width="30%">
          <template slot-scope="{row,$index}">
            <el-button type="text" @click="handleUpdate(row)">
              <i class="el-icon-edit-outline" />编辑
            </el-button>
            <el-popover :ref="'popover-' + row.roleCode" placement="top" width="160" title="删除" trigger="click">
              <div>
                <el-button size="mini" @click="$refs[`popover-` + row.roleCode].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="$refs[`popover-` + row.roleCode].doClose(); handleDelete(row,$index)">确定</el-button>
              </div>
              <el-button slot="reference" type="text" style="margin-left: 10px">
                <i class="el-icon-delete" />删除
              </el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
  </div>
</template>

<script>
import { queryRoleCode, updateSysUrl, queryDataFieldByMenuCode, deleteDataField, authorizeMenuDataToRole } from '@/api/authority'
import qs from 'qs'

export default {
  name: '',
  components: {
  },
  directives: {},
  filters: {},
  data() {
    return {
      basicData: {
        columns: [
          { config: { title: '菜单名称', field: 'menuName' }, type: 'tree' },
          { config: { title: '菜单编码', field: 'menuCode' }},
          { config: { title: '菜单图标', field: 'menuIcon', align: 'center', width: '100px' },
            type: 'render',
            render: (h, scope) => {
              return (
                <div style='display: inline-block'>
                  <i class={scope.row.menuIcon}></i>
                </div>
              )
            }
          },
          { config: { title: '菜单路由', field: 'menuPath' }},
          { config: { title: '菜单排序', field: 'menuSequence' }},
          { config: { title: '操作', width: '100', field: 'opt' }, type: 'render',
            render: (h, scope) => {
              return (
                <div>
                  <el-button plain size='mini' type='warning' v-permission="'authorize'" onClick={() => this.handleAuthorize(scope.row.menuCode)}><i class='el-icon-s-check'></i> 授权</el-button>
                </div>
              )
            }
          }
        ],
        searchData: {
          menuName: null,
          menuPath: null,
          currentPage: 1,
          pageSize: 10
        },
        searchForm: [
          { type: 'Input', label: '菜单名称', prop: 'menuName', width: '180px' },
          { type: 'Input', label: '菜单路由', prop: 'menuPath', width: '180px' }
        ],
        searchHandle: []

      },
      orgName: '',
      temp: {
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        roleCode: [{ required: true, message: '请选择角色编码！', trigger: ['blur', 'change'] }],
        dataField: [{ required: true, message: '请输入过滤数据字段！', trigger: ['blur', 'change'] }]
      },
      downloadLoading: false,
      drawerTableVisible: false,
      checkAll: false,
      checkedRoles: [],
      roleFieldDataSourceList: [],
      roleCodeList: [],
      isIndeterminate: true,
      menuCode: ''
    }
  },
  computed: {},
  created() {},
  mounted() {
  },
  methods: {
    resetTemp() {
      this.temp = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.menuCode = this.menuCode
          authorizeMenuDataToRole(qs.stringify(this.temp)).then(response => {
            this.dialogFormVisible = false
            queryDataFieldByMenuCode(this.menuCode).then(response => {
              this.roleFieldDataSourceList = response.data.list
              this.roleFieldDataSourceCount = response.data.count
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateSysUrl(tempData).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.resetList()
          })
        }
      })
    },
    handleDelete(row, index) {
      this.temp.roleCode = row.roleCode
      this.temp.menuCode = this.menuCode
      deleteDataField(qs.stringify(this.temp)).then(response => {
        this.$message({
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.roleFieldDataSourceList.splice(index, 1)
      })
    },
    handleAuthorize(menuCode) {
      this.menuCode = menuCode // copy obj
      queryRoleCode().then(response => {
        this.roleCodeList = response.data
      })
      queryDataFieldByMenuCode(menuCode).then(response => {
        this.roleFieldDataSourceList = response.data.list
        this.roleFieldDataSourceCount = response.data.count
      })
      this.drawerTableVisible = true
    }
  }
}
</script>

<style scoped>
.warning{
  text-align: center;
  font-size: 20px;
}
.el-icon-info{
  font-size: 30px;
  color: orange;
}
</style>
